﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace Base64Converter
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main(String[] args)
        {
            if (args.Length == 0)
            {
                MessageBox.Show("No parameters given! Use '-d <path>' for decoding or '-e <file>' for encoding.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            if (args[0].ToLower() == "-d")
            {
                if (args.Length < 2)
                {
                    MessageBox.Show("No second parameter given!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(-1);
                }

                Decode(args[1]);
            }
            else if (args[0].ToLower() == "-e")
            {
                if (args.Length < 2)
                {
                    MessageBox.Show("No second parameter given!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(-1);
                }

                Encode(args[1]);
            }
            else
            {
                MessageBox.Show("No known parameters given! Use '-d <path>' for decoding or '-e <file>' for encoding.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }
        }

        private static void Encode(String fileName)
        {
            try
            {
                byte[] buffer = File.ReadAllBytes(fileName);
                String b64 = Convert.ToBase64String(buffer);

                Clipboard.SetText(b64);
            }
            catch (IOException e)
            {
                MessageBox.Show("Error while reading file: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FormatException e)
            {
                MessageBox.Show("Cannot encode file content to Base64: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private static void Decode(String path)
        {
            if (!Clipboard.ContainsText())
            {
                MessageBox.Show("Clipboard has no text data!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = path;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (FileStream fs = new FileStream(dlg.FileName, FileMode.Create))
                    {
                        byte[] buffer = Convert.FromBase64String(Clipboard.GetText());
                        fs.Write(buffer, 0, buffer.Length);
                    }
                }
                catch (IOException e)
                {
                    MessageBox.Show("Error while saving file: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (FormatException e)
                {
                    MessageBox.Show("Cannot decode text from clipboard as Base64: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
